package com.wiloon.androidx.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Common Utilities。<br>
 */
public class CommonUtils {

    /**
     * Get system date format
     */
    public static DateFormat getDateFormat(Context context) {
        return android.text.format.DateFormat.getDateFormat(context);
    }

    /**
     * Get system time format
     */
    public static DateFormat getTimeFormat(Context context) {
        return android.text.format.DateFormat.getTimeFormat(context);
    }

    /**
     * Check if array contains string
     */
    public static String match(String[] strings, String string) {
        if (strings != null && strings.length > 0) {
            for (String str : strings) {
                if (str != null && str.length() > 0) {
                    if (str.startsWith(string)) {
                        return str;
                    }
                }
            }
        }
        return null;
    }

    /**
     * Add string to array
     */
    public static String[] add(String[] strings, String string) {

        if (strings == null) {
            String[] localString = new String[1];
            localString[0] = string;
            return localString;
        }
        List<String> list = new ArrayList<String>(Arrays.asList(strings));
        list.add(string);
        return list.toArray(new String[list.size()]);
    }

    /**
     * Remove string to array
     */
    public static String[] remove(String[] strings, String string) {
        List<String> list = new ArrayList<String>(Arrays.asList(strings));
        list.remove(string);
        return list.toArray(new String[list.size()]);
    }

    /**
     * Get real path
     */
    public static String getRealImagePath(Activity activity, Uri uri) {
        if (uri.toString().startsWith("content")) {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = activity.managedQuery(uri, proj, null, null, null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else if (uri.toString().startsWith("file")) {
            return uri.toString().substring(7, uri.toString().length());
        } else {
            return "";
        }
    }

    /**
     * Get file name
     */
    public static String getFileName(String path) {
        if (path != null && path.length() > 0) {
            int index = path.lastIndexOf("/");
            index = Math.max(path.lastIndexOf("\\"), index);
            if (index > 0) {
                return path.substring(index + 1);
            }
        }
        return path;
    }

    /**
     * Kill process
     */
    public static void killProcess(Context context, String packageName) {
        int version = android.os.Build.VERSION.SDK_INT;
        ActivityManager manager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        if (version <= 7) {
            manager.restartPackage(packageName);
        } else {
            List<RunningAppProcessInfo> process = manager
                    .getRunningAppProcesses();
            int len = process.size();
            for (int i = 0; i < len; i++) {
                if (process.get(i).processName.equals(packageName)) {
                    android.os.Process.killProcess(process.get(i).pid);
                }
            }
        }
    }

    /**
     * Get file's extension name
     */
    public static String getExtensionName(String fileName) {
        if (fileName != null) {
            int index = fileName.lastIndexOf(".");
            if (index >= 0) {
                return fileName.substring(index + 1);
            }
            return CommonConstants.BLANK;
        }
        return null;
    }

    /**
     * Get file's extension name
     */
    public static String getRandomFileName(String extension,
                                           String defaultExtension) {
        String fileName = UUID.randomUUID().toString();
        if (extension != null && extension.length() > 0) {
            fileName = fileName + "." + extension;
        } else if (defaultExtension != null && defaultExtension.length() > 0) {
            fileName = fileName + "." + defaultExtension;
        }
        return fileName;
    }

    /**
     * Compare strings
     */
    public static boolean equals(String src, String dst) {
        if (src == null || src.length() == 0) {
            if (dst == null || dst.length() == 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return src.equals(dst);
        }
    }

    /**
     * Get a arrangement of quotation name and quotation content
     */
    public static String[] splitQuotation(String str, String token) {
        String[] quoteArray = new String[2];
        if (str != null && str.length() > 0) {
            int index = str.indexOf(token);
            if (index >= 0) {
                quoteArray[0] = str.substring(0, index).trim();
                quoteArray[1] = str.substring(index + 1).trim();
            }
        }
        return quoteArray;
    }

    /**
     * Get Declared Fields
     */
    public static Field[] getDeclaredFields(Class<?> clazz) {
        Map<String, Object> fieldMap = getDeclaredFieldMap(clazz);
        return fieldMap.values().toArray(new Field[fieldMap.size()]);
    }

    /**
     * Get Declared Fields
     */
    protected static Map<String, Object> getDeclaredFieldMap(Class<?> clazz) {
        Map<String, Object> fieldMap = new HashMap<String, Object>();
        if (clazz.getSuperclass() != null) {
            fieldMap.putAll(getDeclaredFieldMap(clazz.getSuperclass()));
        }
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            fieldMap.put(field.getName(), field);
        }
        return fieldMap;
    }

    /**
     * stringtoBitmap
     *
     * @param string
     * @return bitmap
     */
    public static Bitmap transportStringtoBitmap(String string) {
        // change String to Bitmap
        Bitmap bitmap = null;
        try {
            byte[] bitmapArray;
            bitmapArray = Base64.decode(string, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                    bitmapArray.length);
        } catch (Exception e) {
            Log.e("CommonUtils", "transportStringtoBitmap", e);
        }

        return bitmap;
    }

    /**
     * bitmaptoString
     *
     * @param bitmap
     * @return string
     */
    public static String transportBitmaptoString(Bitmap bitmap, String format) {
        CompressFormat compressFormat = CompressFormat.JPEG;
        if (format != null && "PNG".equals(format.toUpperCase())) {
            compressFormat = CompressFormat.PNG;
        }
        // change Bitmap to String
        String string = null;
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        bitmap.compress(compressFormat, 100, bStream);
        byte[] bytes = bStream.toByteArray();
        try {
            bStream.close();
        } catch (IOException e) {
            Log.e("CommonUtils", "transportStringtoBitmap", e);
        } finally {
            if (bStream != null) {
                try {
                    bStream.close();
                } catch (IOException e) {
                    Log.e("CommonUtils", "transportStringtoBitmap", e);
                }
            }
        }
        // string = Base64.encodeToString(bytes, Base64.DEFAULT);

        return Base64.encodeToString(bytes, 0, bytes.length, Base64.DEFAULT);
    }

    /**
     * 将文件转成base64 字符串
     *
     * @param path
     * @return *
     * @throws Exception
     */

    public static String fileToBase64(String path) {
        File file = new File(path);
        FileInputStream inputFile = null;
        try {
            inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();
            return Base64.encodeToString(buffer, 0, buffer.length,
                    Base64.DEFAULT);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
        }
        return null;
    }

    /**
     * set image's bottom corners to round<BR>
     *
     * @param bitmap
     * @param roundPx
     * @author cuijk
     */
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(0, -20, bitmap.getWidth(),
                bitmap.getHeight());

        final Paint paint = new Paint();
        int color = 0xff424242;
        paint.setColor(color);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    /**
     * set image's bottom corners to round<BR>
     *
     * @param bitmap
     * @param roundPx
     * @author zhuf
     */
    public static Bitmap getRoundedAllCornerBitmap(Bitmap bitmap, float roundPx) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(0, 0, bitmap.getWidth(),
                bitmap.getHeight());

        final Paint paint = new Paint();
        int color = 0xff424242;
        paint.setColor(color);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Get File Name
     */
    public static String getImageName(String filePath) {
        if (filePath != null) {
            int index = filePath.lastIndexOf("/");
            String name = CommonConstants.BLANK;
            if (index >= 0) {
                name = filePath.substring(index + 1);
                int index2 = name.lastIndexOf(".");
                if (index >= 0) {
                    name = name.substring(0, index2);
                }
                return name;
            }
            return name;
        }
        return null;
    }

    public static void saveTmpImgUrl(Context context, String imgUri) {
        SharedPreferences sp = context.getSharedPreferences("SP",
                Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("imgUri", imgUri);
        editor.commit();
    }

    public String getTmpImgUri(Activity activity) {
        SharedPreferences sp = activity.getSharedPreferences("SP",
                Activity.MODE_PRIVATE);
        String value = sp.getString("imgUri", "");
        return value;
    }
}
